"""
种子银行相关模块
"""
from src import image, text
from src.plants.seed.seed_card import SeedCard

# 太阳数量文本位置
SUN_NUM_TEXT_POSITION_X = 20
SUN_NUM_TEXT_POSITION_Y = 61
# 种子卡片的大小
SEED_WIDTH = 50
SEED_HEIGHT = 65
SEED_SIZE = (SEED_WIDTH, SEED_HEIGHT)
# 种子卡片位置
SEED_POSITION_X_START = 25
SEED_POSITION_Y_START = 10
SEED_POSITION_GAP = 9


class SeedBank(object):
    """
    种子银行类
    """
    # 图片
    __img = None
    # 出现加载动画执行所需的时间
    __loading_duration = 20
    # 出现加载动画已经执行的时间
    __loading_time = 0
    # 加载动画每帧向下移动的距离
    __height_per_second = 0
    # 位置
    __position = (0, 0)
    # 太阳数
    __sun_num = 0
    # 种子银行中种子个数
    __seed_count = 6
    # 植物种子卡片列表
    __seed_card_list = []

    def __init__(self, loading_duration=20, seed_count=6):
        """
        初始化种子银行对象
        :param loading_duration: 种子银行加载动画执行的时长
        """
        # 种子银行图片
        self.__img = image.seed_bank
        # 种子银行图片的高度
        img_h = self.__img.get_height()
        # 加载动画执行时长
        self.__loading_duration = loading_duration
        # 加载动画每帧向下移动的距离
        self.__height_per_second = img_h / loading_duration
        # 初始位置
        self.__position = (0, -img_h)
        # 种子个数
        self.__seed_count = seed_count
        # 生成种子格子
        seed_position_x_start = SEED_POSITION_X_START
        for i in range(seed_count):
            seed_position_x_start += SEED_WIDTH + SEED_POSITION_GAP
            self.__seed_card_list.append(SeedCard(
                SEED_SIZE,
                (seed_position_x_start, SEED_POSITION_Y_START)
            ))

    def draw(self):
        """
        在窗口中绘制种子银行
        """
        self.__loading()

    def __loading(self):
        """
        种子银行加载动画
        :return:
        """
        image.draw.draw_img_with_pre_handle(
            self.__img,
            self.__position,
            img_draw_to_win_pre_handler=self.__draw_pre_handler
        )
        # 向下移动
        if self.__loading_time < self.__loading_duration:
            self.__position = (0, self.__position[1] + self.__height_per_second)
            self.__loading_time += 1

    def __draw_pre_handler(self, seed_bank, seed_bank_size):
        """
        绘制种子银行到窗口前的预处理
        :return:
        """
        self.__draw_sun_to_bank(seed_bank)
        self.__draw_seed_to_bank(seed_bank)

    def __draw_sun_to_bank(self, seed_bank):
        """
        绘制太阳数量文本到种子银行中
        :param seed_bank: 种子银行 Surface 对象
        :return:
        """
        sun_num_text_obj = text.draw.gen_text_obj(str(self.__sun_num), color=(0, 0, 0), is_bold=True)
        seed_bank.blit(sun_num_text_obj, (SUN_NUM_TEXT_POSITION_X, SUN_NUM_TEXT_POSITION_Y))

    def __draw_seed_to_bank(self, seed_bank):
        """
        绘制植物种子到种子银行中
        :param seed_bank: 种子银行 Surface 对象
        :return:
        """
        for i in range(self.__seed_count):
            seed_img = None
            seed_card = self.__seed_card_list[i]
            seed_card.set_img(seed_img)
            seed_card.draw_to(seed_bank)

    def set_seed_card(self, idx, seed_name):
        """
        设置种子卡片
        :param idx:
        :param seed_name:
        :return:
        """
        if idx < 0 or idx >= self.__seed_count:
            return
        seed_card = self.__seed_card_list[idx]
        seed_card.set_img_name(seed_name)
